﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CommonMvc.Common
{
    public interface ICookieManager
    {      
        void SetCookie(string name, string value);
        void SetCookie(string name, string value, DateTimeOffset expires);
        void SetCookie(string name, string value, string domain);
        /// <summary>
        /// 添加cookie 
        /// </summary>
        /// <param name="name">cookie 名称</param>
        /// <param name="value">cookie值</param>
        /// <param name="domain">cookie 作用域</param>
        /// <param name="expires">cookie 过期时间</param>
        void SetCookie(string name, string value, string domain, DateTimeOffset expires);
        void ClearCookie(string name);
        string GetCookieValue(string name);
    }


    public class CookieManager : ICookieManager
    {
        private readonly HttpContext _httpContext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpContext">HTTP context</param>
        public CookieManager(IHttpContextAccessor httpContext)
        {
            this._httpContext = httpContext.HttpContext;
        }

       
        public void SetCookie(string name, string value)
        {
            CookieOptions cookieOptions = new CookieOptions { };
            SetCookie(name, value, cookieOptions);
        }
        public void SetCookie(string name, string value, string domain)
        {
            CookieOptions cookieOptions = new CookieOptions
            {
                 Domain = domain
            };
            SetCookie(name, value, cookieOptions);
           
        }
        public void SetCookie(string name, string value, DateTimeOffset expires)
        {

            CookieOptions cookieOptions = new CookieOptions {
                Expires = expires
            };
            SetCookie(name, value, cookieOptions);          
        }
        public void SetCookie(string name, string value, string domain, DateTimeOffset expires)
        {
            CookieOptions cookieOptions = new CookieOptions
            {
                Domain = domain,
                Expires = expires
            };
            SetCookie(name, value, cookieOptions);
        }
        //根据Key获取cookie
        public string GetCookieValue(string name)
        {
            if (_httpContext.Request.Cookies != null && _httpContext.Request.Cookies[name] != null)
            {
                return _httpContext.Request.Cookies[name];
            }
            return string.Empty;
        }

        //cookie删除
        public void ClearCookie(string name)
        {       
            if (_httpContext.Request.Cookies != null && _httpContext.Request.Cookies[name] != null)
            {
                _httpContext.Response.Cookies.Delete(name);
            }
        }
        //Cookie发送回客户端
        private void SetCookie(String name,String value,CookieOptions options)
        {
            _httpContext.Response.Cookies.Delete(name);
            _httpContext.Response.Cookies.Append(name,value,options);
            
        }
    }
}
